<?php

use PHPUnit\Framework\TestCase;
use Nece\Sdk\Coze\Api\Chat;

class ChatTest extends TestCase
{
    /**
     * @var Chat
     */
    protected $chat;

    protected function setUp(): void
    {
        $client = Bootstrap::getCozeClient();
        $this->chat = $client->chat();
    }

    /**
     * 测试发起对话
     *
     * @author gjw
     * @created 2025-04-24 16:10:27
     *
     * @return array
     */
    public function testChat()
    {
        $conversation_id = Bootstrap::$conversation_id;
        $bot_id = Bootstrap::$bot_id;
        $user_id = 'test_user_' . time();
        
        $result = $this->chat->chat($conversation_id, $bot_id, $user_id);

        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
        $this->assertArrayHasKey('id', $result['data']);

        // 返回对话ID和会话ID，供后续测试使用
        return [
            'conversation_id' => $conversation_id,
            'chat_id' => $result['data']['id']
        ];
    }

    /**
     * 测试查看对话详情
     *
     * @author gjw
     * @created 2025-04-24 16:12:09
     * @depends testChat
     *
     * @param array $ids
     * @return array
     */
    public function testRetrieve(array $ids)
    {
        $result = $this->chat->retrieve($ids['conversation_id'], $ids['chat_id']);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
        $this->assertArrayHasKey('data', $result);
        
        return $ids;
    }

    /**
     * 测试查看对话消息详情
     *
     * @author gjw
     * @created 2025-04-24 16:13:45
     * @depends testRetrieve
     *
     * @param array $ids
     * @return array
     */
    public function testList(array $ids)
    {
        $result = $this->chat->list($ids['conversation_id'], $ids['chat_id']);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        $this->assertEquals(0, $result['code']);
        
        return $ids;
    }

    /**
     * 测试提交工具执行结果
     *
     * @author gjw
     * @created 2025-04-24 16:15:22
     * @depends testList
     *
     * @param array $ids
     * @return array
     */
    public function testSubmitToolOutputs(array $ids)
    {
        // 假设有工具执行结果
        $tool_outputs = [
            [
                'tool_call_id' => 'tool_call_' . time(),
                'output' => json_encode(['result' => 'success'])
            ]
        ];
        
        $result = $this->chat->submitToolOutputs($ids['conversation_id'], $ids['chat_id'], $tool_outputs);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        // 由于可能没有实际的工具调用，所以不一定返回成功码
        // $this->assertEquals(0, $result['code']);
        
        return $ids;
    }

    /**
     * 测试取消进行中的对话
     *
     * @author gjw
     * @created 2025-04-24 16:17:03
     * @depends testSubmitToolOutputs
     *
     * @param array $ids
     * @return void
     */
    public function testCancel(array $ids)
    {
        $result = $this->chat->cancel($ids['conversation_id'], $ids['chat_id']);
        
        $this->assertIsArray($result);
        $this->assertArrayHasKey('code', $result);
        // 由于对话可能已经完成，所以不一定能取消成功
        // $this->assertEquals(0, $result['code']);
    }
}